﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

namespace PocketPC_AdventureGame
{
    //screen
    public class Screen
    {
        public readonly Image background;
        public readonly GameObject foreground;
        public List<GameObject> screenObjects = new List<GameObject>();
        public List<Exit> exits = new List<Exit>();
        private Location[,] locationMap;

        public Screen(Bitmap _foreground, Bitmap _background, Bitmap _locationMap)
        {
            background = _background;
            foreground = new GameObject("Foreground", _foreground);
            locationMap = CreateLocationMap(_locationMap);
        }


        /// <summary>
        /// Get the location object at coordinates, or null if there is coordinates are not reachable. 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public Location GetLocatationAt(int x, int y)
        {
            return locationMap[(x + 4) / 10, (y + 4) / 10];

        }

        /// <summary>
        /// Get closest reachable location to coordinates.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public Location FindClosestLocation(int x, int y)
        {

            Location closest = GetLocatationAt(x, y);   //see if position is reachable

            //find location closest to coordinates
            if (closest == null)
            {
                int mapX = (x + 4) / 10;
                int mapY = (y + 4) / 10;
                double closestDistanceSoFar = 999999;

                foreach (Location loc in locationMap)
                {
                    if (loc != null)
                    {
                        double distance2 = (mapX - loc.x) * (mapX - loc.x) + (mapY - loc.y) * (mapY - loc.y);
                        if (distance2 < closestDistanceSoFar)
                        {
                            closest = loc;
                            closestDistanceSoFar = distance2;
                        }
                    }
                }
            }
            return closest;
        }

        /// <summary>
        /// Create a map of location objects based on grayscale bitmap.
        /// </summary>
        /// <param name="locMap"></param>
        /// <returns></returns>
        private Location[,] CreateLocationMap(Bitmap locMap)
        {
            Location[,] lMap = new Location[locMap.Width, locMap.Height];
            for (int y = 0; y < locMap.Height; y++)
            {
                for (int x = 0; x < locMap.Width; x++)
                {
                    byte distance = locMap.GetPixel(x, y).R;
                    if (distance > 0)
                    {
                        Location l = new Location(x, y);
                        l.distance = distance;
                        if (x > 0 && x < locMap.Width && lMap[x - 1, y] != null)
                        {
                            l.West = lMap[x - 1, y];
                            lMap[x - 1, y].East = l;
                        }
                        if (y > 0 && y < locMap.Height && lMap[x, y - 1] != null)
                        {
                            l.North = lMap[x, y - 1];
                            lMap[x, y - 1].South = l;
                        }
                        lMap[x, y] = l;
                    }
                }
            }
            return lMap;
        }
    }

    public class Location
    {
        public Location(int _x, int _y)
        {
            x = _x;
            y = _y;
        }
        public Location West;
        public Location North;
        public Location East;
        public Location South;
        public byte distance;
        public int x, y;
    }

}
